package com.example.administrator.android2019.ch3Activity;

import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.android2019.R;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class Ch3FileOuterActivity extends AppCompatActivity {
    private Button btn1,btn2;
    private TextView tv;
    String fileName="hello txt";

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ch3_file_outer);
        btn1=findViewById(R.id.ch3_file_outer_btn_read);
        btn2=findViewById(R.id.ch3_file_outer_btn_write);
        tv=findViewById(R.id.ch3_file_outer_tv);

        String [] p={"android.perisslon.WRITE_EXTERNAL_STORAGE"};
        ActivityCompat.requestPermissions(Ch3FileOuterActivity.this,p,1);
        View.OnClickListener click=new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()){
                    case 1:
                        read();
                        break;
                    case 2:
                        write();
                        break;
                }
            }

            };
        btn1.setOnClickListener(click);
        btn2.setOnClickListener(click);
    }
    private void write() {


        String message="Android\nIos";
        /*获取外部设备的状态*/
     String device=Environment.getExternalStorageState();
     /*MEDIA_MOUNTED 存储媒体已经挂载，并且挂载点可读/写*/
     if (device.equals(Environment.MEDIA_MOUNTED)){
         /*获取外部存储设备的路径*/
         File path=Environment.getExternalStorageDirectory();
         /*file 存储文件*/
         File file=new File(path,"h2.text");
         /*通过IO流的方式将message中的内容 写入到file文件*/
         FileOutputStream out= null;
         BufferedWriter bw=null;
         try {
             out=new FileOutputStream(file);
             bw=new BufferedWriter(new OutputStreamWriter(out));
             bw.write(message);
             bw.flush();
             Toast.makeText(this,"写入成功！",Toast.LENGTH_SHORT).show();

         } catch (Exception e) {
             e.printStackTrace();
         }

     }
    }

    private void read() {
        String device=Environment.getExternalStorageState();
        if (device.equals(Environment.MEDIA_MOUNTED)){
            File path=Environment.getExternalStorageDirectory();
            File file=new File(path,"h2.text");
            FileInputStream in=null;
            BufferedReader br=null;
            try {
                in=new FileInputStream(file);
                br=new BufferedReader(new InputStreamReader(in));
                String line=null;
                String s="";
                while ((line=br.readLine())!=null){
                    s+=line+"\n";
                }
                tv.setText(s);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
    }
